<template>
  <div class="app-main-container">
    <div class="secondColor" style="margin-bottom: 20px;">{{ $t('message.zmpglgz') }}</div>
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clear"
    />
    <div v-if="meta.indexOf('TicketAdd') > -1" class="doumee_table_header">
      <el-button
        v-if="meta.indexOf('TicketAdd') > -1"
        v-preventReClick
        type="primary"
        @click="addTicket"
      >{{ $t('message.add') }}</el-button>
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table doumee-element-tableb"
    >
      <el-table-column
        align="center"
        :label="$t('message.mpmc')"
        prop="name"
        min-width="130"
        show-overflow-tooltip
      >
        <template v-slot="scope">
          <span class="primaryColor pointer" @click="handleDetail(scope.row)">{{
            scope.row.name
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$t('message.syxm')"
        prop="projectName"
        min-width="140"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.mpjg')"
        prop="price"
        min-width="80"
      />
      <el-table-column
        align="center"
        :label="$t('message.xsqd')"
        prop="channelName"
        min-width="140"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="$t('message.xssd')"
        min-width="180"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{
            scope.row.saleDateStart.slice(0, 10) +
              " ~ " +
              scope.row.saleDateEnd.slice(0, 10)
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$t('message.rffsl')"
        prop="circulationDay"
        min-width="100"
      />
      <el-table-column align="center" :label="$t('message.piaoyan')" min-width="80">
        <template slot-scope="scope">
          <el-image v-if="scope.row.ticketPic" :preview-src-list="[scope.row.ticketPic]" style="width: 40px" :src="scope.row.ticketPic" alt="" />
          <img v-else style="width: 40px" src="@/assets/images/default_space.png">
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$t('message.smzt')"
        prop="saleStatus"
        min-width="80"
      >
        <template slot-scope="scope">
          <span
            :class="
              scope.row.saleStatus == '1' ? 'sale-color' : 'stop-sale-color'
            "
          >{{ scope.row.saleStatus == "1" ? $t('message.zaishou') : $t('message.tishou') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="$t('message.status')"
        min-width="70"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :disabled="meta.indexOf('TicketStatus') == -1"
            :active-value="'1'"
            :inactive-value="'0'"
            @change="upDateStatus(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('message.caozuo')"
        :width="$i18n.locale === 'ZH'? 150 : 250"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button
              v-if="meta.indexOf('TicketEdit') > -1 && scope.row.status != '1'"
              v-preventReClick
              type="text"
              @click="toEdit(scope.row)"
            >{{ $t('message.edit') }}</el-button>
            <el-button
              v-if="meta.indexOf('TicketAdd') > -1"
              v-preventReClick
              type="text"
              @click="toEdit(scope.row, 'copy')"
            >{{ $t('message.copy') }}</el-button>
            <el-button
              v-if="meta.indexOf('TicketDel') > -1 && scope.row.status != '1'"
              v-preventReClick
              type="text"
              class="stop-sale-color"
              @click="deleteTicket(scope.row.id)"
            >{{ $t('message.del') }}</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div>
      <Pagination
        :total="totalCount"
        :page="pagination.page"
        @current-change="getList"
        @SizeChange="handleSizeChange"
      />
    </div>
    <TicketDetail v-if="isShowDetail" ref="TicketDetailRef" @close="isShowDetail = false" />
  </div>
</template>

<script>
import {
  ticketList,
  getProjectList,
  updateTicketStatus,
  removeTicket,
  ticketDiscount
} from '@/api'
import { parseTime } from '@/utils'
import { queryFormConfig } from './config'
import Pagination from '@/components/paginations'
import QueryForm from '@/components/QueryForm'
import TicketDetail from '../ticketDetail'
export default {
  name: 'TicketList',
  components: {
    QueryForm,
    Pagination,
    TicketDetail
  },
  data() {
    return {
      isShowDetail: false,
      meta: [],
      list: [],
      totalCount: 10,
      listLoading: false,
      discountInfo: '',
      pagination: {
        page: 1,
        rows: 10
      },
      queryFormConfig,
      projectList: [],
      sellStatusList: [
        { value: '1', label: this.$t('message.zaishou') },
        { value: '2', label: this.$t('message.tishou') }
      ],
      statusList: [
        { value: '1', label: this.$t('message.qy') },
        { value: '0', label: this.$t('message.jy') }
      ],
      filters: {}
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.getProjectList()
    this.getList(1)
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.getList()
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  methods: {
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    getProjectList() {
      getProjectList({
        param: { venueId: sessionStorage.getItem('venueId'), type: '1' }
      })
        .then((res) => {
          if (res.errorCode === '000000') {
            const arr = res.recordList
            arr.forEach((item) => {
              item.label = item.projectName
              item.value = item.id
            })
            this.queryFormConfig.formItems[1].options = arr
          }
        })
        .catch((err) => {
          console.log(err)
        })
    },
    getList(page) {
      const { filters, pagination } = this
      if (page) {
        pagination.page = page
        this.pagination.page = Number(page)
      }
      pagination.firstQueryTime = parseTime(new Date())
      this.listLoading = true
      ticketList({
        pagination,
        param: {
          ...filters,
          venueId: sessionStorage.getItem('venueId')
        }
      })
        .then((res) => {
          this.listLoading = false
          if (res.errorCode === '000000') {
            this.list = res.recordList
            this.totalCount = res.totalCount
          }
        })
        .catch(() => {
          this.listLoading = false
        })
    },
    clear() {
      this.filters = { time: [] }
      this.getList(1)
    },
    toShow(scope) {
      // 展示折扣详情
      // list
      ticketDiscount({ param: { ticketId: scope.row.id }}).then((res) => {
        if (res.errorCode === '000000') {
          this.discountInfo = res.record.discountInfo
        }
      })
    },
    toEdit(row, type) {
      // 编辑门票
      this.$router.push({ name: 'TicketAdd', query: { id: row.id, type, isSale: row.isSale || '0' }})
    },
    handleDetail(row) {
      this.isShowDetail = true
      this.$nextTick(() => {
        this.$refs.TicketDetailRef.isShow = true
        this.$refs.TicketDetailRef.getTicketDetail(row.id)
      })
      // this.$router.push({ name: 'TicketDetail', query: { id: row.id }})
    },
    upDateStatus(row) {
      // 更新门票状态
      const arr = this.list
      const obj = { ...row }
      this.listLoading = true
      updateTicketStatus({
        param: {
          ticketId: obj.id,
          status: obj.status
        }
      })
        .then((res) => {
          this.listLoading = false
          if (res.errorCode === '000000') {
            if (obj.status === '1') {
              this.$message.success(this.$t('message.qycg'))
            } else {
              this.$message.success(this.$t('message.jycg'))
            }
            this.getList()
          } else {
            arr.forEach((e) => {
              if (e.id === obj.id) {
                e.status = row.status === '1' ? '0' : '1'
              }
            })
            this.list = arr
          }
        })
        .catch((err) => {
          this.listLoading = false
          arr.forEach((e) => {
            if (e.id === obj.id) {
              e.status = row.status === '1' ? '0' : '1'
            }
          })
          this.list = arr
          console.log(err)
        })
    },
    deleteTicket(id) {
      // 删除门票
      this.$confirm(this.$t('message.sptc'), this.$t('message.ts'), {
        confirmButtonText: this.$t('message.qd'),
        cancelButtonText: this.$t('message.qx'),
        type: 'warning'
      })
        .then(() => {
          removeTicket({
            param: {
              ticketId: id
            }
          })
            .then((res) => {
              if (res.errorCode === '000000') {
                this.getList()
              }
            })
            .catch((err) => {
              console.log(err)
            })
        })
        .catch(() => {
          // this.$message.info('已取消删除')
        })
    },
    addTicket() {
      this.$router.push({ name: 'TicketAdd' })
    }
  }
}
</script>

<style lang="scss" scoped>
.pre {
  white-space: pre;
  .discount-title {
    color: #303133;
    font-size: 16px;
    margin-bottom: 12px;
  }
}
.sale-color {
  color: #4aa468;
}
.stop-sale-color {
  color: #ed4d00;
}
</style>
